enum SceneLoadState {
	Unloaded = 0,
	Loading = 1,
	Loaded = 2,
	Unloading = 3,
}

class SceneManager {
	public static isInBattle: boolean = false;
	private static _instance: SceneManager;
	public uiRoot: paper.GameObject;
	public uiBackRoot: paper.GameObject;

	private quanxiState: SceneLoadState = SceneLoadState.Unloaded;
	private quanxiScene: paper.Scene;

	public static get instance(): SceneManager {
		if (!SceneManager._instance) {
			SceneManager._instance = new SceneManager();
		}
		return SceneManager._instance;
	}

	private _sceneArray = [];

	/**
	 * 加载场景(有资源加载)
	 */
	private async loadScene(name: string): Promise<any> {
		let res = UrlConfig.sceneRoot + name + ".scene.json";
		await RES.getResAsync(res);
		if (GameUtils.isEmpty(this.uiRoot)) {
			await this.createGameUI();
		}
		// if (GameUtils.isEmpty(this.uiBackRoot)) {
		// 	await this.createGameBackUI();
		// }
		return new Promise((resolve, reject) => {
			//延迟加载下一个场景，防止加载上一个场景没有释放完全问题
			resolve(res);
		});
	}

	private releaseAll() {
		let _array = [];
		for (let i = 0; i < this._sceneArray.length; i++) {
			_array.push(this._sceneArray[i]);
		}
		for (let i = 0; i < _array.length; i++) {
			this.releaseScene(_array[i]);
		}
	}

	/**
	 * 释放场景
	 */
	public releaseScene(name: string) {
		// LayerManager.instance.removeAll();
		let scene: paper.Scene = paper.Application.sceneManager.getScene(name);
		if (scene)
			paper.Application.sceneManager.unloadScene(scene);
		if (this._sceneArray.indexOf(name) > -1) {
			this._sceneArray.splice(this._sceneArray.indexOf(name), 1);
		};
	}

	/**
	 * 获取场景是否显示
	 */
	public getSceneIsActive(name: string): boolean {
		if (this._sceneArray.indexOf(name) > -1) {
			return true;
		}
		return false;
	}

	public async loadBegin<T extends WatcherView>(clazz?: { new (): T }): Promise<any> {
		this.releaseAll();
		SceneManager.isInBattle = false;
		let res = await this.loadScene("SampleScene");
		let scene = paper.Scene.create(res);
		this._sceneArray.push("SampleScene");
		if (clazz) {
			// LayerManager.instance.newNormal(clazz, CleanType.ALL);
		}
		//非战斗场景开启背景UI摄像机(2D、3D展示叠加用)
		if (this.uiBackRoot)
			this.uiBackRoot.activeSelf = false;
	}

	/**
	 * 创建UI根节点
	 */
	public async createGameUI() {
		this.uiRoot = paper.GameObject.create();
		this.uiRoot.name = "GameUI";
		this.uiRoot.dontDestroy = true;
		this.uiRoot.layer = paper.Layer.UI;

		let uiCamera = this.uiRoot.addComponent(egret3d.Camera);
		// uiCamera.cullingMask = paper.CullingMask.UI;
		uiCamera.cullingMask = paper.Layer.UI;//TODO
		uiCamera.order = 1;
		uiCamera.clearOption_Color = false;
		const renderer = this.uiRoot.addComponent(egret3d.Egret2DRenderer);
		const adapter = new egret3d.MatchWidthOrHeightAdapter();
		adapter.setResolution(750, 1334);
		console.log(egret3d.stage.size.w + "," + egret3d.stage.size.h);
		adapter.matchFactor = 1;
		renderer.screenAdapter = adapter;

		let assetAdapter = new AssetAdapter();
		egret.registerImplementation("eui.IAssetAdapter", assetAdapter);
		egret.registerImplementation("eui.IThemeAdapter", new ThemeAdapter());

		return new Promise((resolve, reject) => {
			let theme = new eui.Theme("resource/default.thm.json", renderer.stage);
			theme.addEventListener(eui.UIEvent.COMPLETE, function () {
				let render = this.uiRoot.getComponent(egret3d.Egret2DRenderer) as egret3d.Egret2DRenderer;
				let stageLayer = new GuiLayer();
				render.root.addChild(stageLayer);
				resolve();
			}, this);
		});

	}

	/**
	 * 创建UI背景节点(只有枪展示页面需要)
	 */
	public async createGameBackUI() {
		this.uiBackRoot = paper.GameObject.create();
		this.uiBackRoot.name = "GameBackUI";
		this.uiBackRoot.dontDestroy = true;
		// this.uiBackRoot.layer = paper.Layer.UserLayer1;
		this.uiBackRoot.layer = paper.Layer.UserLayer12;//TODO

		let uiCamera = this.uiBackRoot.addComponent(egret3d.Camera);
		// uiCamera.cullingMask = paper.CullingMask.UserLayer1;
		uiCamera.cullingMask = paper.Layer.UserLayer12;//TODO
		uiCamera.order = -1;
		const renderer = this.uiBackRoot.addComponent(egret3d.Egret2DRenderer);
		const adapter = new egret3d.MatchWidthOrHeightAdapter();
		adapter.setResolution(750, 1334);
		adapter.matchFactor = 1;
		renderer.screenAdapter = adapter;


		let assetAdapter = new AssetAdapter();
		egret.registerImplementation("eui.IAssetAdapter", assetAdapter);
		egret.registerImplementation("eui.IThemeAdapter", new ThemeAdapter());

		return new Promise((resolve, reject) => {
			let theme = new eui.Theme("resource/default.thm.json", renderer.stage);
			theme.addEventListener(eui.UIEvent.COMPLETE, function () {
				resolve();
			}, this);
		});

	}
}